<template>
    <div class="admin-paiban">
        <div class="admin-paiban-case">
            <div class="paiban-top">
                <div class="paiban-top-left">
                    <div class="paiban-top-left-title">
                        选择排班科室医生
                    </div>
                    <div class="paiban-top-left-search">
                        <el-select style="width:200px;margin-right:12px" v-model="ksform.bh" placeholder="请选择科室">
                            <el-option v-for="(item,i) in vbmbjArr" :key="i" :label="item.mc" :value="item.bh"></el-option>
                        </el-select>
                        <el-input style="width:200px;margin-right:12px" v-model="ksform.ryxm" placeholder="请输入医生姓名"></el-input>
                        <el-button type="primary" :icon="Search" @click="searchClick" style="width:72px">查询</el-button>
                    </div>
                        <!-- 表格 -->
                    <xinpuTableGrid
                        ref="xinpuGrid"
                        :editConfig = "editConfig"
                        :loading="loading"
                        :tableHeight="tableHeight"
                        :tableWidth="tableWidth"
                        :columnsData="columns"
                        :data="data"
                        :border="true"
                    >
                    </xinpuTableGrid>
                </div>
                <div class="paiban-top-middle">
                    <div class="paiban-top-middle-title">
                        选择排班班次
                    </div>
                    <div style="cursor: pointer;" @click="banciguanli" class="paiban-top-middle-middle">
                        <img src="../../../assets/images/banci_icon.png" style="width:16px;height:16px;margin-right:4px" alt="" srcset="">
                        <div>班次管理</div>
                    </div>
                    <xinpuTableGrid
                        ref="xinpuGridbanci"
                        :loading="loadingbanci"
                        :tableHeight="tableHeightbanci"
                        :tableWidth="tableWidthbanci"
                        :columnsData="columnsbanci"
                        :data="databanci"
                        :border="true"
                    >
                    </xinpuTableGrid>
                </div>
                <div class="paiban-top-right">
                    <div class="paiban-top-right-title">
                        选择排班日期
                    </div>
                    <div style="padding:12px;box-sizing: border-box;border-left: 1px solid #EBEBEB;border-right: 1px solid #EBEBEB;border-bottom: 1px solid #EBEBEB;height:411px;border-radius:0 0 4px 4px">
                        <div class="paiban-top-right-btn">
                            <div class="paiban-top-right-btn-left">
                                <div>{{Year}}年</div>
                                <div @click="last()" class="paiban-top-right-btn-right" style="width:32px;height:32px;margin-right:14px;margin-left:14px">
                                    <el-icon color="#999999 " class="no-inherit">
                                        <ArrowLeft />
                                    </el-icon>
                                </div>
                                <div>{{month}}月</div>
                                <div @click="next()" class="paiban-top-right-btn-right" style="width:32px;height:32px;margin-left:14px">
                                    <el-icon color="#999999 " class="no-inherit">
                                        <ArrowRight />
                                    </el-icon>
                                </div>
                            </div>
                            <div @click="reset" class="paiban-top-right-btn-right">
                                今天
                            </div>
                        </div>
                        <div class="paiban-top-right-week">
                            <div class="paiban-top-right-week-btn" v-for="(item,i) in weekArr" :key="i">{{ item }}</div>
                        </div>
                        <div class="wrap">
                            <div
                                class="span"
                                v-for="(item, index) in calendarList"
                                :key="index"
                                @click="click(item.count)"
                                :class="
                                item == ''
                                    ? 'kong'
                                    : item.count < nowtime
                                    ? 'noclick'
                                    : (item.count >= starttime && item.count <= endtime) ||
                                    item.count == starttime
                                    ? 'active'
                                    : ''
                                "
                            >
                                {{ item.value }}
                            </div>
                        </div>
                    </div> 
                </div>
            </div>     
            <div class="generate-case">
                <div class="generate-case-box">
                    <div>总号源数量</div>
                    <el-input style="width:100px;margin-left:8px;margin-right:12px" v-model="form.hyzs" placeholder="请输入"></el-input>
                </div>
                <div class="generate-case-box">
                    <div>预留号源数量</div>
                    <el-input style="width:100px;margin-left:8px;margin-right:12px" v-model="form.hys_dd" placeholder="请输入"></el-input>
                </div>
                <div class="generate-case-box">
                    <div>号源类型</div>
                    <selectCheckedTable
                        style="width:100px;margin-left:8px;margin-right:12px"
                        v-model:data="form.hylx_id"
                        :fields="hylxArrFields"
                        :tableData="hylxArr"
                        objKey="itemid"
                        label="itemid"
                        :border="true"
                        :alreadyOption="hylxYxArr"
                        alreadyOptionKey="value"
                        alreadyOptionLabel="label"
                        clearable
                        @update="selectCheckedChange"
                    ></selectCheckedTable>
                </div>
                <div @click="generateSchedule" class="generate-case-btn">
                    <img src="../../../assets/images/shengcheng_icon.png" style="width:16px;height:16px" alt="" srcset="">
                    <div>生成排班表</div>
                </div>
            </div>
            <div class="paiban-bottom">
                <div class="paiban-bottom-search">
                    <div style="margin-right:8px">时间</div>
                    <div style="width:220px;margin-right:12px">
                        <el-date-picker
                            v-model="formSearch.value"
                            type="datetimerange"
                            range-separator="至"
                            style="width:220px;"
                            value-format="YYYY-MM-DD"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                        />
                    </div>
                    <div style="margin-right:8px">科室</div>
                    <el-select style="width:200px;margin-right:12px" v-model="formSearch.bmbm" placeholder="请选择">
                        <el-option v-for="(item,i) in vbmbjArr" :key="i" :label="item.mc" :value="item.bh"></el-option>
                    </el-select>
                    <div style="margin-right:8px">医生</div>
                    <el-input style="width:200px;margin-right:12px" v-model="formSearch.ysbm" placeholder="请输入"></el-input>
                    <div style="margin-right:8px">就诊状态</div>
                    <el-select style="width:200px;margin-right:12px" v-model="formSearch.jzzt" placeholder="请选择">
                        <el-option label="正常" value="0"></el-option>
                        <el-option label="停诊" value="1"></el-option>
                        <el-option label="换诊" value="2"></el-option>
                        <el-option label="转入" value="3"></el-option>
                        <el-option label="所有" value="-1"></el-option>
                    </el-select>
                    <el-button type="primary" :icon="Search" @click="searchPaibanClick" style="width:72px">查询</el-button>
                </div>
                <div class="paiban-bottom-btn">
                    <img
                        src="../../../assets/images/save_icon.png"
                        style="width: 16px; height: 16px; margin-right: 2px"
                        alt=""
                        srcset=""
                    />
                    <div @click="savepaiban" style="margin-right:12px">保存排班</div>
                    <img
                        src="../../../assets/images/referral_icon.png"
                        style="width: 16px; height: 16px; margin-right: 2px"
                        alt=""
                        srcset=""
                    />
                    <div style="margin-right:12px">转诊</div>
                    <img
                        src="../../../assets/images/der_icon.png"
                        style="width: 16px; height: 16px; margin-right: 2px"
                        alt=""
                        srcset=""
                    />
                    <div style="margin-right:12px">导出</div>
                    <img
                        src="../../../assets/images/tingzhen_icon.png"
                        style="width: 16px; height: 16px; margin-right: 2px"
                        alt=""
                        srcset=""
                    />
                    <div @click="closeClick" style="margin-right:12px">停诊</div>
                    <img
                        src="../../../assets/images/tingzhen_icon.png"
                        style="width: 16px; height: 16px; margin-right: 2px"
                        alt=""
                        srcset=""
                    />
                    <div @click="cancelCloseClick" style="margin-right:12px">取消停诊</div>
                    <img
                        src="../../../assets/images/del_icon.png"
                        style="width: 16px; height: 16px; margin-right: 2px"
                        alt=""
                        srcset=""
                    />
                    <div @click="deletepaibandibu" style="margin-right:12px">删除</div>
                    <img
                        src="../../../assets/images/paibanxinxi_icon.png"
                        style="width: 16px; height: 16px; margin-right: 2px"
                        alt=""
                        srcset=""
                    />
                    <div style="margin-right:12px">排班信息上传</div>
                    <img
                        src="../../../assets/images/tinzhenxinxi_icon.png"
                        style="width: 16px; height: 16px; margin-right: 2px"
                        alt=""
                        srcset=""
                    />
                    <div style="margin-right:12px">停诊信息上传</div>
                </div>
                <xinpuTableGrid
                    ref="xinpuGriddibu"
                    :loading="loadingdibu"
                    :tableHeight="tableHeightdibu"
                    :tableWidth="tableWidthdibu"
                    :columnsData="columnsdibu"
                    :data="datadibu"
                    :border="true"
                    @updataSelect="updataSelect"
                >
                </xinpuTableGrid>
            </div>
        </div>
      <el-dialog
        v-model="dialogVisible"
        draggable
        style="border-radius:4px"
        width="800px"
        :show-close="false"
        :modal="false"
      >
        <template #header>
          <div class="dialog-header">
            <div class="dialog-header-left">
              <img src="../../../assets/images/bancicolor_icon.png" style="width:16px;height:16px;margin-right:8px;" alt="" srcset="">
              <div>班次管理</div>
            </div>
            <el-icon @click="banciguanliquxiao" style="cursor: pointer;" color="#fff" :size="18">
              <Close />
            </el-icon>
          </div>
        </template>
        <div class="dialog-content">
          <div class="dialog-content-top">
            <div>名称<el-input style="width:200px;margin-right:12px;margin-left:8px" v-model="name" placeholder="请输入"></el-input><el-button type="primary" :icon="Search" @click="searchbanciClick" style="width:72px">查询</el-button></div>
          </div>
          <div class="dialog-content-line"></div>
          <div class="button-case">
            <div @click="addData" class="button-case-box">
              <img
                src="../../../assets/images/add_icon.png"
                style="width: 16px; height: 16px; margin-right: 2px"
                alt=""
                srcset=""
              />
              <div>新增</div>
            </div>
            <div @click="deleteClick" class="button-case-box">
              <img
                src="../../../assets/images/del_icon.png"
                style="width: 16px; height: 16px; margin-right: 2px"
                alt=""
                srcset=""
              />
              <div>删除</div>
            </div>
            <div @click="saveClick" class="button-case-box">
              <img
                src="../../../assets/images/save_icon.png"
                style="width: 16px; height: 16px; margin-right: 2px"
                alt=""
                srcset=""
              />
              <div>保存</div>
            </div>
          </div>
          <xinpuTableGrid
              ref="xinpuGridbanciadd"
              :loading="loadingbanciadd"
              :tableHeight="tableHeightbanci"
              :tableWidth="tableWidthbanci"
              :columnsData="columnsbanciadd"
              :data="databanciadd"
              :border="true"
          >
          </xinpuTableGrid>
        </div>
      </el-dialog>
    </div>
</template>

<script setup>
import selectCheckedTable from "@/components/selectCheckedTable/index.vue";
import { ElMessage } from 'element-plus'
import { ref, onMounted } from "vue";
import {Search} from '@element-plus/icons-vue'
const dialogVisible = ref(false)
const name = ref('')
const xinpuGridbanciadd = ref('')
const loadingbanciadd = ref(false)
const databanciadd = ref([])
const columnsbanciadd = ref([
  { type: "checkbox", width: 48 },
  { field: "workname", title: "名称", width: 100, editRender: { name: "input" } },
  { field: "starttime", title: "开始时间",editRender: {},selectName:'timeSelect'},
  { field: "endtime", title: "结束时间",editRender: {},selectName:'timeSelect'},
  { field: "sfty", 
    title: "是否停用",
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "dm",
          label: "代码",
          width: 100,
        },
        {
          prop: "mc",
          label: "名称",
          width: 200,
        }
      ],
      data: [
        {
          dm:'0',
          mc:'否'
        },
        {
          dm:'1',
          mc:'是'
        }
      ],
      objKey:'dm',
      label:'mc'
    },
  },
  { field: "remark", title: "备注", editRender: { name: "input" }},
])
const form = ref({
    hyzs:'100',
    hys_dd:'0',
    hylx_id:''
})
const editConfig = ref({
    trigger: 'click', 
    mode: 'cell', 
    showIcon: false,
    beforeEditMethod ({ row }) {
        return false
    }
})
const ksform = ref({
  bh:'',
  ryxm:''
})
const loading = ref(false)
const tableHeight = ref(370)
const tableWidth = ref(630)
const vbmbjArr = ref([])
const columns = ref([
  { type: "checkbox", width: 48 },
  { field: "bmbm_1", 
    title: "科室", 
    width: 220,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "bh",
          label: "代码",
          width: 100,
        },
        {
          prop: "mc",
          label: "名称",
          width: 200,
        }
      ],
      data: [],
      objKey:'bh',
      label:'mc'
    },
  },
  { field: "ryxm", title: "医生", width: 100 },
  { field: "ghlb",
    title: "挂号类别",
    width: 120,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "value",
          label: "代码",
          width: 100,
        },
        {
          prop: "label",
          label: "名称",
          width: 200,
        }
      ],
      data: [],
      objKey:'value',
      label:'label'
    },
  },
  { field: "zsbm",
    title: "诊室",
    width: 120,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "bh",
          label: "代码",
          width: 100,
        },
        {
          prop: "mc",
          label: "名称",
          width: 200,
        }
      ],
      data: [],
      objKey:'bh',
      label:'mc'
    },
  },
])
const data = ref([])
const xinpuGrid = ref(null)
const xinpuGridbanci = ref(null)
const loadingbanci = ref(false)
const tableHeightbanci = ref(370)
const tableWidthbanci = ref(document.body.clientWidth - 254 - 630 - 24 - 552)
const columnsbanci = ref([
  { type: "checkbox", width: 48 },
  { field: "workname", title: "名称", width: 100 },
  { field: "starttime", title: "开始时间"},
  { field: "endtime", title: "结束时间"},
])
const databanci = ref([])

const hylxArr = ref([])
const hylxArrFields = ref([
    {
        prop: "value",
        label: "代码",
        width: 100,
    },
    {
        prop: "label",
        label: "名称",
        width: 200,
    }
])
const hylxYxArr = ref([])
const loadingdibu = ref(false)
const tableHeightdibu = ref(document.body.clientWidth - 1634)
const tableWidthdibu = ref(document.body.clientWidth - 254)
const columnsdibu = ref([
  { type: "checkbox", width: 48 },
  { field: "id", title: "编号", width: 70 },
  { field: "hylx_id",
    title: "号源类型", 
    width: 100,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "value",
          label: "代码",
          width: 100,
        },
        {
          prop: "label",
          label: "名称",
          width: 200,
        }
      ],
      data: [],
      objKey:'value',
      label:'label'
    },
  },
  { field: "workdate", title: "日期", width: 120},
  { 
    field: "workaddr",
    title: "诊室",
    width: 120,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "bh",
          label: "代码",
          width: 100,
        },
        {
          prop: "mc",
          label: "名称",
          width: 200,
        }
      ],
      data: [],
      objKey:'bh',
      label:'mc'
    },
  },
  { 
    field: "ysbm", 
    title: "医生", 
    width: 100,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "value",
          label: "代码",
          width: 100,
        },
        {
          prop: "label",
          label: "名称",
          width: 200,
        }
      ],
      data: [],
      objKey:'value',
      label:'label'
    },
  },
  { field: "ghlb",
    title: "挂号类别",
    width: 120,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "value",
          label: "代码",
          width: 100,
        },
        {
          prop: "label",
          label: "名称",
          width: 200,
        }
      ],
      data: [],
      objKey:'value',
      label:'label'
    },
  },
  { 
    field: "ksbm", 
    title: "科室", 
    width: 220,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "bh",
          label: "代码",
          width: 100,
        },
        {
          prop: "mc",
          label: "名称",
          width: 200,
        }
      ],
      data: [],
      objKey:'bh',
      label:'mc'
    },
  },
  { field: "worktimecode", title: "出诊时段", width: 100 },
  { field: "jzkssj", title: "开始时间", width: 100 },
  { field: "jzjssj", title: "结束时间", width: 100 },
  { field: "hyzs", title: "总数", width: 100, editRender: { name: "input" } },
  { field: "hys_dd", title: "预留", width: 100, editRender: { name: "input" } },
  { field: "jzh", title: "预约挂号数", width: 100, editRender: { name: "input" } },
  { field: "registrationcount", title: "未确认", width: 100, editRender: { name: "input" } },
  { 
    field: "jzzt", 
    title: "就诊状态", 
    width: 100,
    editRender: {},
    selectName:'selectTable',
    selectOption: {
      fields: [
        {
          prop: "value",
          label: "代码",
          width: 100,
        },
        {
          prop: "label",
          label: "名称",
          width: 200,
        }
      ],
      data: [
        {
          label:'正常',
          value:'0'
        },
        {
          label:'停诊',
          value:'1'
        },
        {
          label:'换诊',
          value:'2'
        },
        {
          label:'转入',
          value:'3'
        },
        {
          label:'所有',
          value:'-1'
        },
      ],
      objKey:'value',
      label:'label'
    },
  },
  { field: "reason", title: "原因", width: 100},
  { field: "xgr", title: "修改人", width: 100},
  { field: "xgrq", title: "修改日期", width: 180},
])
const datadibu = ref([])
const xinpuGriddibu = ref(null)

const weekArr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六',]
const nowtime = ref('') 
const clickitem = ref(0)
const clickcount = ref(0)
const starttime = ref('')
const endtime = ref('')
const Year = ref(new Date().getFullYear())
const month = ref(new Date().getMonth() + 1)
const Day = ref(new Date().getDate())
const nowYear = ref(new Date().getFullYear())
const nowmonth = ref(new Date().getMonth() + 1)
const nowDay = ref(new Date().getDate())

const calendarList = ref([])
const formSearch = ref({
    value:[],
    jzzt:'0',
    ysbm:'',
    bmbm:''
})
const banciguanli = ()=>{
  dialogVisible.value = true
  searchbanciClick()
}
const addData = ()=>{
  databanciadd.value.push({
    endtime: "",
    id: 0,
    remark: '',
    saveFlag: 1,
    sfty: 0,
    starttime: "",
    workname: "",
    _X_ROW_KEY: ""
  })
}
const deleteClick = async()=>{
  const arr = xinpuGridbanciadd.value.removeTable();
  console.log(arr);
  if(arr && arr.length>0){
    let result = []
    arr.forEach(item=>{
      if(item.saveFlag == 1){
        databanciadd.value.forEach((v,j)=>{
          if(item._X_ROW_KEY == v._X_ROW_KEY){
            databanciadd.value.splice(j,1)
          }
        })
      }else{
        item.saveFlag = 3
        result.push(item)
      }
    })
    if(result.length>0){
      const res = await window.xinpuHttpApi('200059',{'list':result},'mzhttphosptalserver')

      if(res && res.Flag){
        ElMessage({
            message: '删除成功',
            type: 'success',
        })
        searchbanciClick()
      }
    }else{
      ElMessage({
          message: '删除成功',
          type: 'success',
      })
    }
  }else{
    ElMessage({
        message: '请选择要删除的数据',
        type: 'warning',
    })
  }
}
const saveClick = async()=>{
  const arr = xinpuGridbanciadd.value.getEditData();
  console.log(arr);
  if(arr && arr.length>0){
    arr.forEach(item=>{
      if(item.saveFlag !== 1){
        item.saveFlag = 2
      }
    })
    const res = await window.xinpuHttpApi('200059',{'list':arr},'mzhttphosptalserver')
    if(res && res.Flag){
      ElMessage({
          message: '保存成功',
          type: 'success',
      })
      searchbanciClick()
    }
  }else{
    ElMessage({
        message: '当前没有要保存和新增的数据',
        type: 'warning',
    })
  }
}
const banciguanliquxiao = ()=>{
  dialogVisible.value = false
  name.value = ''
  getSchedulingRule()
}
const searchbanciClick = async()=>{
  loadingbanciadd.value = true
  const res = await window.xinpuHttpApi('200058',{'name':name.value},'mzhttphosptalserver')
  console.log(res);
  if(res && res.Flag){
    databanciadd.value = res.ReContent
  }
  loadingbanciadd.value = false
}
const savepaiban = async()=>{
  let flag = false
  datadibu.value.forEach(v=>{
    if(v.saveFlag == 1){
      flag = true
    }
  })
  if(flag){
    const res = await window.xinpuHttpApi('200052',{'list':datadibu.value},'mzhttphosptalserver')
    if(res && res.Flag){
      datadibu.value = []
      ElMessage({
          message: '保存排班成功',
          type: 'success',
      })
    }
  }else{
    const arr = xinpuGriddibu.value.getEditData();
    arr.forEach(v=>{
      v.saveFlag = 2
    })
    const res = await window.xinpuHttpApi('200052',{'list':arr},'mzhttphosptalserver')
    if(res && res.Flag){
      datadibu.value = []
      ElMessage({
          message: '保存排班成功',
          type: 'success',
      })
    }
  }
}
const searchClick = ()=>{
    getData()
}
const cancelCloseClick = async()=>{
  const item = xinpuGriddibu.value.getCurrentEvent();
  console.log(item);
  if(item){
    let flag = false
    if(item.id == 0){
      flag = true
    }
    if(flag){
      ElMessage({
          message: '数据尚未保存，请保存后取消停诊',
          type: 'warning',
      })
    }else{
      item.jzzt = '0'
      const res = await window.xinpuHttpApi('200053',item,'mzhttphosptalserver')
      console.log(res);
      if(res && res.Flag){
        datadibu.value = []
        ElMessage({
            message: '转诊成功',
            type: 'success',
        })
      }
    }
  }else{
    ElMessage({
        message: '请点击你要取消停诊的数据',
        type: 'warning',
    })
  }
}
const closeClick = async()=>{
  const item = xinpuGriddibu.value.getCurrentEvent();
  console.log(item);
  if(item){
    let flag = false
    if(item.id == 0){
      flag = true
    }
    if(flag){
      ElMessage({
          message: '数据尚未保存，请保存后停诊',
          type: 'warning',
      })
    }else{
      item.jzzt = '1'
      const res = await window.xinpuHttpApi('200053',item,'mzhttphosptalserver')
      console.log(res);
      if(res && res.Flag){
        datadibu.value = []
        ElMessage({
            message: '转诊成功',
            type: 'success',
        })
      }
    }
  }else{
    ElMessage({
        message: '请点击你要停诊的数据',
        type: 'warning',
    })
  }
}
const deletepaibandibu = ()=>{
  const arr = xinpuGriddibu.value.removeTable();
  console.log(arr);
  console.log(datadibu.value);
  let flag = false
  arr.forEach(v=>{
    if(v.id == 0){
      flag = true
    }
  })
  if(flag){
    arr.forEach(it=>{
      datadibu.value.forEach((item,i)=>{
        if(it._X_ROW_KEY == item._X_ROW_KEY){
          datadibu.value.splice(i,1)
        }
      })
    })
  }else{
    ElMessage({
        message: '已经保存的不能删除，请修改或者停诊',
        type: 'warning',
    })
  }
}
const searchPaibanClick = async()=>{
  loadingdibu.value = true
  const res = await window.xinpuHttpApi('200145',{'ysbm':formSearch.value.ysbm,'bmbm':formSearch.value.bmbm,'jzzt':[formSearch.value.jzzt],'vda1':formSearch.value.value[0],'vda2':formSearch.value.value[1]},'mzhttphosptalserver')
  console.log(res);
  if(res && res.Flag){
    datadibu.value = res.ReContent
  }
  loadingdibu.value = false
}

const generateSchedule = ()=>{
    const item = xinpuGrid.value.removeTable();
    console.log(item);
    if(item && item.length>0){
        const itembanci = xinpuGridbanci.value.removeTable();
        console.log(itembanci);
        if(itembanci && itembanci.length>0){
            let nowtimes = nowtime.value.slice(0, 4) + '-' + nowtime.value.slice(4, 6) + '-' +  nowtime.value.slice(6)
            let starttimes = starttime.value.slice(0, 4) + '-' + starttime.value.slice(4, 6) + '-' +  starttime.value.slice(6)
            let endtimes = endtime.value.slice(0, 4) + '-' + endtime.value.slice(4, 6) + '-' +  endtime.value.slice(6)
            let arrs = getDatesInRange(starttimes,endtimes)
            if(arrs && arrs.length>0){
              if(form.value.hylx_id && form.value.hylx_id.split(',').length>1 ){
                let hylx_idArr = form.value.hylx_id.split(',')
                console.log(hylx_idArr,'直行道这了吗？');
                datadibu.value = []
                hylx_idArr.forEach(jss=>{
                  item.forEach(it=>{
                    itembanci.forEach(ite=>{
                      arrs.forEach(v=>{
                        datadibu.value.push({
                          ghlb:it.ghlb,
                          hylx_id:jss,
                          hys_dd:'0',
                          hyzs:form.value.hyzs,
                          id:0,
                          jzh:'0',
                          jzjssj:ite.endtime,
                          jzkssj:ite.starttime,
                          jzzt:'0',
                          ksbm:it.bmbm_1,
                          reason:'',
                          registrationcount:'0',
                          saveFlag:'1',
                          workaddr:it.zsbm,
                          workdate:v,
                          workid:'2',
                          worktimecode:ite.workname,
                          xgr:'修改人',
                          xgrq:nowtimes,
                          ysbm:it.rybm,
                          zdr:'0000',
                          zdrq:nowtimes,
                          _X_ROW_KEY:''
                        })
                      })
                    })
                  })
                })
              }else{
                datadibu.value = []
                  item.forEach(it=>{
                    itembanci.forEach(ite=>{
                      arrs.forEach(v=>{
                        datadibu.value.push({
                          ghlb:it.ghlb,
                          hylx_id:form.value.hylx_id,
                          hys_dd:'0',
                          hyzs:form.value.hyzs,
                          id:0,
                          jzh:'0',
                          jzjssj:ite.endtime,
                          jzkssj:ite.starttime,
                          jzzt:'0',
                          ksbm:it.bmbm_1,
                          reason:'',
                          registrationcount:'0',
                          saveFlag:'1',
                          workaddr:it.zsbm,
                          workdate:v,
                          workid:'2',
                          worktimecode:ite.workname,
                          xgr:'修改人',
                          xgrq:nowtimes,
                          ysbm:it.rybm,
                          zdr:'0000',
                          zdrq:nowtimes,
                          _X_ROW_KEY:''
                        })
                      })
                    })
                  })
              }
            }
        }else{
            ElMessage({
                message: '请选择班次',
                type: 'warning',
            })
        }
    }else{
        ElMessage({
            message: '请选择排班科室医生',
            type: 'warning',
        })
    }
}

const Draw = (Year, Month)=>{
    //日期列表
    var calendar = [];

    //用当月第一天在一周中的日期值作为当月离第一天的天数(获取当月第一天是周几)
    for (var i = 1, firstDay = new Date(Year, Month - 1, 1).getDay(); i <= firstDay; i++) {
        calendar.push("");
    }

    //用当月最后一天在一个月中的日期值作为当月的天数
    for (var i = 1, monthDay = new Date(Year, Month, 0).getDate(); i <= monthDay; i++) {

        let time_month = Month;
        let time_day = i;
        if (Month < 10) {
        time_month = 0 + '' + Month;
        }
        if (i < 10) {
        time_day = 0 + '' + i;
        }

        calendar.push({
        value: i,
        count: Year + '' + time_month + '' + time_day
        })
    }
    calendarList.value = calendar;
}

const last = ()=>{
    month.value--;
    if (month.value == 0) {
        month.value = 12;
        Year.value--;
    }

    Draw(Year.value, month.value);
}

const next = ()=>{
    month.value++;
    if (month.value == 13) {
        month.value = 1;
        Year.value++;
    }

    Draw(Year.value, month.value);
}

const click = (item)=>{
    clickcount.value++;
    clickitem.value = item;

    //开始日期
    if (clickcount.value % 2 == 1) {
        starttime.value = clickitem.value;
        endtime.value = ''
    } else {
        endtime.value = clickitem.value;
        if (starttime.value > endtime.value) {
            endtime.value = starttime.value;
            starttime.value = clickitem.value;
        }
    }
}

const firm = ()=>{}

const cancle = ()=>{
    starttime.value = nowtime.value;
    endtime.value = nowtime.value;
}

const reset = ()=>{
    nowtime.value = ''
    clickitem.value = 0
    clickcount.value = 0
    starttime.value = ''
    endtime.value = ''
    Year.value = new Date().getFullYear()
    month.value = new Date().getMonth() + 1
    Day.value = new Date().getDate()
    nowYear.value = new Date().getFullYear()
    nowmonth.value = new Date().getMonth() + 1
    nowDay.value = new Date().getDate()
    calendarList.value = []
    Draw(nowYear.value,nowmonth.value)
    let time_month = nowmonth.value; //现在的月份
    let time_day = nowDay.value; //现在的天数
    if (nowmonth.value < 10) {
        time_month = 0 + '' + nowmonth.value;
    }
    if (nowDay.value < 10) {
        time_day = 0 + '' + nowDay.value;
    }
    nowtime.value = nowYear.value + '' + time_month + '' + time_day;
    starttime.value = nowtime.value;
    endtime.value = nowtime.value;
}

const getbmData = (async()=>{
  const res = await window.xinpuHttpApi('102001',{})
  if(res && res.Flag){
    columns.value.forEach(v=>{
      if(v.field == 'bmbm_1'){
        v.selectOption.data = res.ReContent
      }
    })
    columnsdibu.value.forEach(v=>{
      if(v.field == 'ksbm'){
        v.selectOption.data = res.ReContent
      }
    })
  }
})

const getghlbData = async ()=>{
  const res = await window.xinpuWebsApi('GetFixCode', { "bh":"xp_mzlb"});
  if(res && res.Flag){
    columns.value.forEach(v=>{
      if(v.field == 'ghlb'){
        v.selectOption.data = res.ReContent
      }
    })
    columnsdibu.value.forEach(v=>{
      if(v.field == 'ghlb'){
        v.selectOption.data = res.ReContent
      }
    })
  }
}

const getzsbmData = async ()=>{
  const res = await window.xinpuHttpApi('100228',{
    "MyPaginationDto":{
        PageNo:1,
        PageSize:9999,
        OrderByname:'bh',
        isDesc:false
    },
    "InParamDXmxx":{
        lsmc:''
    }
  })
  if(res && res.Flag){
    columns.value.forEach(v=>{
      if(v.field == 'zsbm'){
        v.selectOption.data = res.ReContent.Content
      }
    })
    columnsdibu.value.forEach(v=>{
      if(v.field == 'workaddr'){
        v.selectOption.data = res.ReContent.Content
      }
    })
  }
}

// 获取医生人员
const getysbmData = async()=>{
  const res = await window.xinpuWebsApi('GetFixCode', { "bh":"xp_ry_002"});
  console.log(res);
  if(res && res.Flag){
    columnsdibu.value.forEach(v=>{
      if(v.field == 'ysbm'){
        v.selectOption.data = res.ReContent
      }
    })
  }
}

// 获取排班规则
const getSchedulingRule = (async()=>{
  const res = await window.xinpuHttpApi('200058',{'name':name.value},'mzhttphosptalserver')
  console.log(res);
  if(res && res.Flag){
    databanci.value = res.ReContent
  }
})

// 获取号源类型
const gethylx= (async()=>{
  const res = await window.xinpuWebsApi('GetFixCode', { "bh":"xp_hylx"});
  console.log(res);
  if(res && res.Flag){
    hylxArr.value = res.ReContent
    columnsdibu.value.forEach(v=>{
      if(v.field == 'hylx_id'){
        v.selectOption.data = res.ReContent
      }
    })
  }
})


// 下拉选择框修改的数据
const selectCheckedChange = (val, column) => {
  console.log(val);
  if(val){
    hylxYxArr.value = val
    let arr = []
    val.forEach(v=>{
        arr.push(v.value)
    })
    form.value.hylx_id = arr.join(',')
    console.log(form.value.hylx_id);
  }
  
}

// 下拉框需要修改的数据
const updataSelect = (val, column) => {
  if(column.column && column.column.field == 'workaddr'){
    let resultIndex = columnsdibu.value[column.columnIndex].field;
    datadibu.value[column.rowIndex][resultIndex] = val.bh;
  }
  if(column.column && column.column.field == 'ghlb'){
    let resultIndex = columnsdibu.value[column.columnIndex].field;
    datadibu.value[column.rowIndex][resultIndex] = val.value;
  }
};

const getKsbm = async()=>{
  const res = await window.xinpuHttpApi('102001',{'vsyzt':'1'})
  console.log(res);
  if(res && res.Flag){
    vbmbjArr.value = res.ReContent
    columnsdibu.value.forEach(v=>{
      if(v.field == 'ksbm'){
        v.selectOption.data = res.ReContent
      }
    })
  }
}

const getData = (async()=>{
  loading.value = true
  const res = await window.xinpuHttpApi('100345',ksform.value)
  console.log(res);
  let arr = []
  if(res && res.Flag){
    res.ReContent.forEach(item=>{
        if(item.wx_sfxs == 1){ 
            arr.push(item)
        }
    })
    data.value = arr
  }
  loading.value = false
})
const getFormSearch = ()=>{
    // 获取今天的日期
    const today = new Date();
    // 获取一个月之后的日期
    const nextMonth = new Date(today.getFullYear(), today.getMonth() + 1, today.getDate());
    console.log(formatDate(today));
    console.log(formatDate(nextMonth));
    formSearch.value.value[0] = formatDate(today)
    formSearch.value.value[1] = formatDate(nextMonth)
}
// 将日期转换为 "YYYY-MM-DD HH:mm:ss" 格式
function formatDate(date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    const seconds = date.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

function getDatesInRange(startDate, endDate) {
  const dateArray = [];
  let currentDate = new Date(startDate);
 
  while (currentDate <= new Date(endDate)) {
    // 将当前日期的字符串表示添加到数组中
    dateArray.push(currentDate.toISOString().split('T')[0]);
    // 增加一天
    currentDate.setDate(currentDate.getDate() + 1);
  }
 
  return dateArray;
}
onMounted(async() => {
  reset()
  getData()
  getbmData()
  getghlbData()
  getzsbmData()
  getSchedulingRule()
  gethylx()
  getFormSearch()
  getKsbm()
  getysbmData()
});
</script>

<style lang="scss" scoped>
.admin-paiban{
  padding: 8px;
  box-sizing: border-box;
  width: 100%;
  height: calc(100vh - 83px);
  background: #f7f7f7;
  .admin-paiban-case{
    width: 100%;
    height: calc(100vh - 100px);
    background: #fff;
    padding: 12px;
    box-sizing: border-box;
    border-radius: 4px 4px 4px 4px;
    .paiban-top{
        width: 100%;
        height: 474px;
        border-radius: 4px 4px 0px 0px;
        border: 1px solid #EBEBEB;
        padding: 12px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        .paiban-top-left{
            flex: 0 0 630px;
            height: 450px;
            margin-right: 12px;
            .paiban-top-left-title{
                width: 100%;
                height: 40px;
                border: 1px solid #EBEBEB;
                display: flex;
                align-items: center;
                justify-content: flex-start;
                padding-left: 12px;
                box-sizing: border-box;
                font-weight: 400;
                font-size: 14px;
                color: #333333;
                border-radius: 4px 4px 0px 0px;
            }
            .paiban-top-left-search{
                width: 100%;
                height: 40px;
                display: flex;
                align-items: center;
                justify-content: flex-start;
                padding-left: 12px;
                box-sizing: border-box;
                border-left: 1px solid #EBEBEB;
                border-right: 1px solid #EBEBEB;
            }
        }
        .paiban-top-right{
            flex: 0 0 524px;
            height: 450px;
            .paiban-top-right-title{
                width: 100%;
                height: 40px;
                border: 1px solid #EBEBEB;
                display: flex;
                align-items: center;
                justify-content: flex-start;
                padding-left: 12px;
                box-sizing: border-box;
                font-weight: 400;
                font-size: 14px;
                color: #333333;
                border-radius: 4px 4px 0px 0px;
            }
            .paiban-top-right-btn{
                display: flex;
                align-items: center;
                justify-content: space-between;
                .paiban-top-right-btn-left{
                    display: flex;
                    align-items: center;
                    font-weight: bold;
                    font-size: 14px;
                    color: #333333;
                }
                .paiban-top-right-btn-right{
                    width: 52px;
                    height: 32px;
                    border-radius: 4px;
                    border: 1px solid #EBEBEB;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-weight: 400;
                    font-size: 14px;
                    color: #333333;
                }
            }
            .paiban-top-right-week{
                display: flex;
                align-items: center;
                width: 100%;
                margin-top: 12px;
                margin-bottom: 4px;
                .paiban-top-right-week-btn{
                    width: 68px;
                    height: 32px;
                    background: #F7F7F7;
                    border-radius: 4px 4px 4px 4px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-weight: 400;
                    font-size: 12px;
                    color: #333333;
                    margin-right: 4px;
                }
                .paiban-top-right-week-btn:last-child{
                    margin-right: 0 !important;
                }
            }
        }
        .paiban-top-middle{
            flex:1;
            height: 450px;
            margin-right: 12px;
            overflow: hidden;
            .paiban-top-middle-title{
                width: 100%;
                height: 40px;
                border: 1px solid #EBEBEB;
                display: flex;
                align-items: center;
                justify-content: flex-start;
                padding-left: 12px;
                box-sizing: border-box;
                font-weight: 400;
                font-size: 14px;
                color: #333333;
                border-radius: 4px 4px 0px 0px;
            }
            .paiban-top-middle-middle{
                width: 100%;
                height: 40px;
                display: flex;
                align-items: center;
                justify-content: flex-start;
                padding-left: 12px;
                box-sizing: border-box;
                border-left: 1px solid #EBEBEB;
                border-right: 1px solid #EBEBEB;
                font-size: 14px;
                color: #333333;
            }
        }
    }
    .generate-case{
        width: 100%;
        height: 40px;
        border-radius: 0px 0px 4px 4px;
        border-left: 1px solid #EBEBEB;
        border-right: 1px solid #EBEBEB;
        border-bottom: 1px solid #EBEBEB;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        padding-right: 12px;
        box-sizing: border-box;
        .generate-case-box{
            display: flex;
            align-items: center;
            font-size: 14px;
            color: #333333;
        }
        .generate-case-btn{
            width: 114px;
            height: 32px;
            background: #359DE1;
            font-size: 14px;
            color: #FFFFFF;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
        }
    }
    .paiban-bottom{
        margin-top: 12px;
        width: 100%;
        height: calc(100vh - 650px);
        overflow: hidden;
        .paiban-bottom-search{
            width: 100%;
            height: 40px;
            display: flex;
            align-items: center;
            padding-left: 12px;
            box-sizing: border-box;
            border: 1px solid #EBEBEB;
            border-radius: 4px 4px 0 0;
            font-size: 14px;
            color: #333333;
        }
        .paiban-bottom-btn{
            display: flex;
            align-items: center;
            width: 100%;
            height: 40px;
            padding-left: 12px;
            box-sizing: border-box;
            border-left: 1px solid #EBEBEB;
            border-right: 1px solid #EBEBEB;
            font-size: 14px;
            color: #333333;
        }
    }
  }
}
.wrap {
    width: 100%;
    // height: 305px;
    overflow: hidden;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
}
.span {
    width: 68px;
    height: 46px;
    background: #F7F7F7;
    border-radius: 4px 4px 4px 4px;
    color: #333333;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 4px;
    margin-bottom: 5px;
    &.active {
      background: #359DE1;
      color: #fff;
    }
 
    &.noclick {
      pointer-events: none;
      background: #F7F7F7;
      color: #CCCCCC;
    }
 
    &.kong {
      background: #fff;
      pointer-events: none;
    }
  }
.span:nth-child(7n){
    margin-right: 0 !important;
}
.dialog-header{
  width: 100%;
  height: 40px;
  background: #697786;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 17px;
  box-sizing: border-box;
  font-size: 14px;
  color: #FFFFFF;
  border-radius: 4px 4px 0px 0px;
  .dialog-header-left{
    display: flex;
    align-items: center;
  }
}
.dialog-content{
  padding: 12px;
  box-sizing: border-box;
  .dialog-content-top{
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #333333;
  }
  .dialog-content-line{
    width: 100%;
    height: 1px;
    background: #EBEBEB;
    margin: 12px 0;
  }
  .button-case{
    display: flex;
    align-items: center;
    border-top: 1px solid #EBEBEB;
    border-left: 1px solid #EBEBEB;
    border-right: 1px solid #EBEBEB;
    border-radius: 4px 4px 0px 0px;
    height: 40px;
    padding-left: 12px;
    .button-case-box{
      display: flex;
      align-items: center;
      margin-right: 12px;
    }
  }
}
:deep(.el-dialog){
  padding: 0;
  box-sizing: border-box;
}
:deep(.el-dialog__header){
  padding-bottom: 0 !important;
}
</style>